package com.dm.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dm.shop.entity.Address;
import com.dm.shop.entity.User;
import com.dm.shop.service.AddressService;
import com.dm.shop.utils.CommonUtil;
import com.dm.shop.utils.ResultUtil;
import com.dm.shop.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerater-Saiya Wang
 * @since 2021-07-20
 */
@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;
    @GetMapping("/")
    public Result getAddresses(){
        User user= CommonUtil.getCurrentUser();
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",user.getId());
        List<Address> addresses=addressService.list(wrapper);
        return ResultUtil.success(addresses);
    }
    @GetMapping("/getAllAddress/{uid}")
    @ApiOperation("通过用户id,查找该用户的所有收获地址")
    public Result getAllAddress(@PathVariable("uid") int uid){
        return addressService.getAllAddress(uid);
    }
    @PostMapping("/addAddress")
    @ApiOperation("添加收获地址")
    public Result addAddressInfo(@RequestBody Address address){
        return addressService.addAddressInfo(address);
    }
    @PutMapping("/editAddress")
    @ApiOperation("修改收获地址")
    public Result editAddress(@RequestBody Address address){
        return addressService.editAddress(address);
    }
    @DeleteMapping("/del/{id}")
    @ApiOperation("根据地址的id删除地址")
    public Result delAddress(@PathVariable("id") int id){
        return addressService.delAddress(id);
    }
}
